#!/bin/bash
source ./config.sh
nohup nload eth0 -m -t 1000 > ouput  &   
log_full=${log_path}${log_file}

tm=`date '+%Y%m%d %H:%M:%S'`
sleep 2
`ps -ewf | grep nload |grep -v grep| awk '{print $2}'| xargs kill -9 `

head ouput > hh
sed -i "s/^M/\n/g" hh
grep 'Cur' hh |head -n 1 > tt
sed -i 's/^[/ /g' tt


network=`awk '{print " incoming:"$2$3" outgoing:"$5$6}' tt`
mem=`free -m  |grep Mem`
uptime=`uptime`

#alive=`ls -l ${alive_path} |tail -n 1 |awk '{print $9}' |xargs -I {} grep aliveT ${alive_path}{} |tail -n 1|awk '{print $12}' `
while [ -n "${server_name}" ]
do
name=`echo "${server_name}" | cut -d ' ' -f 1`
server_name=`echo "${server_name}" | sed 's/[^ ]* *\(.*\)$/\1/'`
game_log_path=`echo "${alive_path}" | cut -d ' ' -f 1`
alive_path=`echo "${alive_path}" | sed 's/[^ ]* *\(.*\)$/\1/'`
echo ${name}" "${game_log_path}
alive=`ls -l ${game_log_path} |tail -n 1 |awk '{print $9}' |xargs -I {} grep 'current online roles' ${game_log_path}{} |tail -n 1|awk '{print $4}' `
alive=`echo ${alive} | sed 's/^M//g' `
echo ${tm}" "${name}" online "${alive}  >> ${log_full}
done
echo ${tt}
aver=${uptime}

aver=${aver##*:}
aver=${aver%%,*}
max_l='1'

#if [ $(echo "${aver} >= ${max_l}"|bc) = 1 ]
#then
toplog=${log_path}"toplog_"${tm/ /_}
iostatlog=${log_path}"iostat_"${tm/ /_}
top -b -n 1 > ${toplog}.log &
iostat -x 1 5 -n > ${iostatlog}.log &
#fi

#echo ${tm}"    "${alive}  >> ${log_full}
#echo ${tm}"        hjds online "${alive_hjds}  >> ${log_full}
#echo ${tm}"        sxzz online "${alive_tap}  >> ${log_full}

rm hh tt ouput

echo ${tm}" nload"${network} >> ${log_full}

echo ${tm}" free"${mem} >> ${log_full}
echo ${tm}" uptime"${uptime} >> ${log_full}

memlog=${log_path}"mem_"${tm/ /_}
ps aux|sort -rk4|head -n 20 > ${memlog}.log &